<?php
/**
 * --------------------------------
 * # 分页辅助类
 * --------------------------------
 * 1. 2020年5月21日23:04:30
 * 2. ZJY
 * --------------------------------
 */
namespace phpless\ORG;

class Paginator
{
    // 总页数
    public $totals;
    // 每页数
    public $limit;
    // 偏移量
    public $offset;
    // 分页样式
    public $template_type;
    // 可选分页样式
    public $templates_text = array(
        1 => 'simple',
        2 => 'default',
        3 => 'complex',
    );
    // 总页数
    public $page_total;
    // 分页页面变量
    public $page;
    // 当前页
    public $current_page;
    // 上一页
    public $pre_page;
    // 下一页
    public $next_page;
    // 当前 URL 参数
    public $request;
    // 分页器
    public $paginator;
    // 构造方法
    public function __construct($totals = 0, $limit = 15, $template_type = 2, $offset = 2, $pagination_class = '', $page = 'p')
    {
        $this -> totals = (int) $totals;
        $this -> limit = (int) $limit;
        $this -> template_type = ($template_type >= 1 || $template_type <= 3) ? (int) $template_type : 2;
        $this -> offset = (int) (abs($offset));
        $this -> pagination_class = $pagination_class;
        $this -> page = $page;
    }
    // 设置页数
    public function setPage()
    {
        $this -> page_total= ceil($this -> totals / $this -> limit);
    }
    // 设置当前 URL 参数
    public function setRequest()
    {
        $this -> request = $_REQUEST;
    }
    // 设置当前页
    public function setCurrentPage()
    {
        $this -> current_page = isset($this -> request[$this -> page]) && !empty($this -> request[$this -> page]) ? (int) min([abs($this -> request[$this -> page]),$this -> page_total]) : 1;
    }
    // 设置上一页
    public function setPrePage()
    {
        $this -> pre_page = max([$this -> current_page - 1, 1]);
    }
    // 设置下一页
    public function setNextPage()
    {
        $this -> next_page = min([$this -> current_page + 1, $this -> page_total]);
    }
    // 设置分页链接
    public function getLinkUrl($this_page = 0)
    {
        if($this_page > 0){
            // 重置页面
            $this -> request[$this -> page] = $this_page;
            return $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($this -> request);
        }
        return 'javascript:;';
    }
    // 第一种样式：简单样式 simple
    public function getSimpleTemplate()
    {
        $pre_disabled = $this -> current_page == 1 ?'disabled': '';
        $next_disabled = $this -> current_page == $this -> page_total ? 'disabled' : '';
        $paginator = <<<EOF
<nav aria-label="Page navigation">
    <ul class="pager">
        <li class="{$pre_disabled}" title="第{$this -> pre_page}页"><a href="{$this -> getLinkUrl($this -> pre_page)}">上一页</a></li>
        <li class="{$next_disabled}" title="第{$this -> next_page}页"><a href="{$this -> getLinkUrl($this -> next_page)}">下一页</a></li>
    </ul>
</nav>
EOF;
        return $this -> paginator = $paginator;
    }
    // 第二种样式：默认样式 default
    public function getDefaultTemplate()
    {
        $one_page_total = $this -> offset * 2 + 1 + 4;
        if($this -> page_total <= $one_page_total){
            // 全部显示 创建分页数组
            $pager = range(1,$this -> page_total);
        }elseif($this -> page_total > $one_page_total && $this -> current_page - $this -> offset < 4){
            $pager = range(1,$one_page_total - 2);
            $pager[] = 0;
            $pager[] = $this -> page_total;
        }elseif($this -> page_total > $one_page_total && $this -> current_page - $this -> offset >= 4 && $this -> current_page + $this -> offset <= $this -> page_total - 3){
            $pager = range($this -> current_page - $this -> offset, $this -> current_page + $this -> offset);
            array_unshift($pager, 0);
            array_unshift($pager, 1);
            $pager[] = 0;
            $pager[] = $this -> page_total;
        }else{
            $pager = range($this -> page_total - $this -> offset * 2 - 2, $this -> page_total);
            array_unshift($pager, 0);
            array_unshift($pager, 1);
        }
        $li = '';
        foreach ($pager as $v) {
            if ($v == $this -> current_page) {
                $li = "{$li}<li class='active'><a href='javascript:;'>{$v} <span class='sr-only'>(current)</span></a></li>";
            }else{
                $v = $v ?: '...';
                $li = "{$li}<li><a href='{$this -> getLinkUrl($v)}'>{$v}</a></li>";
            }
        }
        $pre_disabled = $this -> current_page == 1 ? 'disabled' : '';
        $next_disabled = $this -> current_page == $this -> page_total ? 'disabled' : '';
        $paginator = <<<EOF
<nav aria-label="Page navigation">
    <ul class="pagination {$this -> pagination_class}">
        <li class="{$pre_disabled}" title="第{$this -> pre_page}页">
            <a href="{$this -> getLinkUrl($this -> pre_page)}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        {$li}
        <li class="{$next_disabled}" title="第{$this -> next_page}页">
            <a href="{$this -> getLinkUrl($this -> next_page)}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
    </ul>
</nav>
EOF;
        return $this -> paginator = $paginator;
    }
    // 第三种样式：复杂样式 complex
    public function getComplexTemplate()
    {
        $one_page_total = $this -> offset * 2 + 1;
        if ($this -> page_total <= $one_page_total && $this -> page_total > 2) {
            // 全部显示 创建分页数组
            $pager = range(2, $this -> page_total - 1);
        } elseif ($this -> current_page > 1 && $this -> current_page < $this -> page_total && $this -> offset == 0) {
            $pager = [$this->current_page];
        }elseif($this -> page_total > $one_page_total && $this -> current_page <= 3){
            $pager = range(2, $one_page_total + 1);
        }elseif($this -> page_total > $one_page_total && $this -> current_page >= 4 && $this -> current_page <= $this -> page_total - 3) {
            $pager = range($this -> current_page - $this -> offset, $this -> current_page + $this -> offset);
        }elseif($this -> page_total > $one_page_total && $this -> current_page >= 4 && $this -> current_page > $this -> page_total - 3){
            $pager = range($this -> page_total - $one_page_total, $this -> page_total - 1);
        }else{
            $pager = [];
        }
        $li = '';
        foreach ($pager as $v) {
            $title = "第{$v}页";
            if ($v == $this -> current_page) {
                $li = "{$li}<li class='active' title='{$title}'><a href='javascript:;'>{$v} <span class='sr-only'>(current)</span></a></li>";
            }else{
                $li = "{$li}<li title='{$title}'><a href='{$this -> getLinkUrl($v)}'>{$v}</a></li>";
            }
        }
        $pre_disabled = $this -> current_page == 1 ? 'disabled' : '';
        $next_disabled = $this -> current_page == $this -> page_total ? 'disabled' : '';
        $first_page_active = $this -> current_page == 1 ? 'active' : '';
        $last_page_active = $this -> current_page == $this -> page_total ? 'active' : '';
        $paginator = <<<EOF
<nav aria-label="Page navigation">
    <ul class="pagination {$this -> pagination_class}">
        <li class="{$pre_disabled}" title="第{$this -> pre_page}页">
            <a href="{$this -> getLinkUrl($this -> pre_page)}" aria-label="Previous">
                <span aria-hidden="true">上一页</span>
            </a>
        </li>
        <li title="首页" class="{$first_page_active}"><a href="{$this -> getLinkUrl(1)}">首页</a></li>
        {$li}
        <li title="第{$this -> page_total}页" class="{$last_page_active}"><a href="{$this -> getLinkUrl($this -> page_total)}">尾页</a></li>
        <li class="to">
            <a href="javascript:;">
                To
            </a>
        </li>
        <li>
            <a href="javascript:;" title="第{$this -> current_page}页">
                <input type="text" name="{$this -> page}" value="" style="height:20px;width:30px;" placeholder="{$this -> current_page}" />
            </a>
        </li>
        <li class="ok">
            <a href="javascript:;">OK</a>
        </li>
        <li class="{$next_disabled}" title="第{$this -> next_page}页">
            <a href="{$this -> getLinkUrl($this -> next_page)}" aria-label="Next">
                <span aria-hidden="true">下一页</span>
            </a>
        </li>
    </ul>
</nav>
EOF;
        return $this -> paginator = $paginator;
    }
    // 获取分页器
    public function render()
    {
        // 判断是否有数据，没有数据则返回空
        if($this -> totals <= 0){
            return false;
        }
        $this -> setPage();
        $this -> setRequest();
        $this -> setCurrentPage();
        $this -> setPrePage();
        $this -> setNextPage();
        // 获取分页器样式
        $template_function = 'get'.ucfirst($this -> templates_text[$this -> template_type]).'Template';
        return $this -> $template_function();
    }
}
